#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/10/12 11:49
@desc
"""

import os
import threading
import urllib2

import queue

from ippool.conf.settings import gen_ippool_dir, check_ip_file_prefix, ping_url, log
# Number of threads
from ippool.lib.pooldao.ip_pool_dao import IpPool
from ippool.lib.pooldao.oper_db import upsert
from ippool.lib.si_time import SiTime

n_thread = 10
# Create queue
queue = queue.Queue()


class ThreadClass(threading.Thread):
    def __init__(self, queue, num):
        threading.Thread.__init__(self)
        # super(ThreadClass, self).__init__()  # Expired
        # Assign thread working with queue
        self.queue = queue
        self.num = num
        self.check_f = self._write_check_file(num)

    def __del__(self):
        self.check_f.close()

    def _write_check_file(self, num):
        file_name = check_ip_file_prefix.format(str(num) + '.txt')
        return open(os.path.join(gen_ippool_dir, file_name), 'w+')

    def run(self):
        while True:
            # Get from queue job
            host = self.queue.get()
            # print(self.getName() + ":" + host)
            hostdict = host.split('=')
            hostdict = (dict(zip(hostdict[0::2], hostdict[1::2])))
            try:
                # print("当前代理IP " + host)
                # proxy = urllib2.ProxyHandler({"http": host})
                proxy = urllib2.ProxyHandler(hostdict)
                opener = urllib2.build_opener(proxy, urllib2.HTTPHandler)
                urllib2.install_opener(opener)
                data = urllib2.urlopen(ping_url).read().decode('utf-8', 'ignore')
                # print("通过")
                log.info('OK %s ', host)
                protocol, host_port = host.split('=')
                print(protocol, host_port, SiTime.now())
                ipObj = IpPool(ip_port=host_port, protocol=protocol, get_time=SiTime.now())
                # print(ipObj)
                # log.info('ipObj:' + str(ipObj))
                upsert(ipObj)
                self.check_f.write(host + '\n')
                self.check_f.flush()
                # print("-----------------------------")
            except Exception as err:
                log.info('REMOVE %s', host)
                # print(err)
                # print("-----------------------------")

            # signals to queue job is done
            self.queue.task_done()


def check_start(orginal_file='proxy_list_20190301102257.txt'):
    # Create number process
    for i in range(n_thread):
        t = ThreadClass(queue, i)
        t.setDaemon(True)
        # Start thread
        t.start()

    # Read file line by line
    wait_check_file = os.path.join(gen_ippool_dir, orginal_file)
    print(wait_check_file)
    hostfile = open(wait_check_file, "r")
    for line in hostfile:
        msg = '='.join((line.split('\t')[1], (line.split('\t')[0])))
        # Put line to queue
        queue.put(msg)
    # wait on the queue until everything has been processed
    queue.join()


if __name__ == '__main__':
    check_start()
